using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DetectCollider : MonoBehaviour
{
    public List<string> ignoreObj = new List<string> { "Tip", "Ground", "Anubis", "Rfoot", "Sphere" };

    public ProcessText dealText;
    private Matrix4x4 curTrans;

    // Start is called before the first frame update
    void Start()
    {
        curTrans = this.transform.GetWorldMatrix();
        if (dealText != null && dealText.IsOptim() && dealText.IsNSMOptim())
        {
            dealText.optimCur.nsmOptimRoutes.Add(curTrans);
        }

    }

    // Update is called once per frame
    void Update()
    {
        curTrans = this.transform.GetWorldMatrix();
        if (dealText != null && dealText.IsOptim() && dealText.IsNSMOptim())
        {
            dealText.optimCur.nsmOptimRoutes.Add(curTrans);
        }
    }

    private void OnTriggerEnter(Collider collider)
    {
        // Only calculate the collision of objects in the scene when the optimized action is executed in the iterative process
        if (dealText == null) return;
        if (!dealText.IsNSMOptim() || ignoreObj.Contains(collider.transform.name) || collider.transform.parent == null) return;
        //if (dealText.optimCur.collisionDepth == null) return;
        if (collider.GetType() != new BoxCollider().GetType()) return;

        var ans = dealText.optimCur.collisionDepth;

        var obj = collider.transform.parent.gameObject;

        // Assuming that the cost of this step is not large, if the cost is large, calculate it when the scene is initially loaded

        if (!ans.ContainsKey(obj))
        {
            var boxcolliders = collider.transform.GetComponents<BoxCollider>();
            ans[obj] = (new HashSet<BoxCollider>(), boxcolliders.Length);
        }
        BoxCollider boxc = collider as BoxCollider;
        
        
        if (!ans[obj].Item1.Contains(boxc))
        {
            ans[obj].Item1.Add(boxc);

            //GameObject trl = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //trl.transform.parent = obj.transform;
            //trl.transform.localPosition = boxc.center;
            //trl.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
            //Material m_tr = trl.GetComponent<Renderer>().sharedMaterial;
            //m_tr.color = Color.green;
            //Debug.Log(obj.name + ": " + boxc.center);
        }
    }


}
